package can.bishe.common.utils;


import can.bishe.modules.exam.entity.ExamOptionEntity;
import can.bishe.modules.exam.entity.ExamSubjectEntity;
import can.bishe.modules.exam.entity.ExamSubjectOptionEntity;
import can.bishe.modules.exam.service.ExamOptionService;
import can.bishe.modules.exam.service.ExamSubjectOptionService;
import can.bishe.modules.exam.service.ExamSubjectService;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@Component
public class SubjectExeclReader {

    private static SubjectExeclReader subjectExeclReader;

    @Autowired
    private ExamOptionService examOptionService;
    @Autowired
    private ExamSubjectService examSubjectService;
    @Autowired
    private ExamSubjectOptionService examSubjectOptionService;
    @PostConstruct
    public void init() {
        subjectExeclReader = this;
    }

    private final Logger log = LoggerFactory.getLogger(getClass());

    public boolean readExcel(String filePath) throws IOException {
        InputStream fileInputStream = null;
        try{
            String FILEPATH = filePath;
            fileInputStream = new FileInputStream(FILEPATH);
            XSSFWorkbook wb = new XSSFWorkbook(fileInputStream);
            int sheetNum = wb.getNumberOfSheets();
            // 获取当前Sheet页
            XSSFSheet sheet = wb.getSheetAt(0);
            int rowNums = sheet.getLastRowNum();
            for(int i = 1; i < rowNums; i += 4){
                ExamSubjectEntity examSubjectOne = new ExamSubjectEntity();
                XSSFRow rowOne= sheet.getRow(i);
                examSubjectOne.setSubjectContent(rowOne.getCell(1).getStringCellValue());
                XSSFCell cells = rowOne.getCell(3);
                cells.setCellType(CellType.STRING);
                examSubjectOne.setSubjectAnswer(cells.getStringCellValue());
                examSubjectOne.setSubjectType("1");
                subjectExeclReader.examSubjectService.save(examSubjectOne);
                for(int j = 0; j < 4; j++ ){
                    ExamOptionEntity examOptionOne = new ExamOptionEntity();
                    XSSFCell cellOne = sheet.getRow(i + j).getCell(2);
                    cellOne.setCellType(CellType.STRING);
                    examOptionOne.setOptionContent(cellOne.getStringCellValue());
                    subjectExeclReader.examOptionService.save(examOptionOne);
                    ExamSubjectOptionEntity examSubjectOptionEntity = new ExamSubjectOptionEntity();
                    examSubjectOptionEntity.setSubjectId(examSubjectOne.getSubjectId());
                    examSubjectOptionEntity.setOptionId(examOptionOne.getOptionId());
                    subjectExeclReader.examSubjectOptionService.save(examSubjectOptionEntity);
                }
            }
        }catch (IOException ioe) {
            log.info("异常信息{}",ioe.getMessage());
            return false;
        }finally {
            fileInputStream.close();
        }
        return true;
    }

}
